package com.easyj.admin.sys.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.easyj.base.common.ajax.Result;
import com.easyj.base.controller.BaseController;
import com.easyj.base.exception.EasyjException;
import com.easyj.admin.db.model.Permission;
import com.easyj.admin.db.model.Role;
import com.easyj.admin.sys.service.PermissionService;
import com.easyj.admin.sys.service.RoleService;
	/**
	* 角色
	*
	 */
	@RestController
	@RequestMapping(value = "/sys/role")
	@PreAuthorize("hasPermission('role','role:all')")
	public class RoleController extends BaseController{

		@Autowired
		private  RoleService roleService;
		
		@Autowired
		private  PermissionService permissionService;


		/**
		 * get
		 * @param id
		 * @return
		 */
	    @GetMapping("/{id}")
	    public Result get(@PathVariable("id") Long id) {
	        Role entity = roleService.findById(id);
	        return Result.Success(entity);
	    }
		
		/**
		* 新增
		 * @throws EasyjException 
		*/
		@RequestMapping(value = "add", method = RequestMethod.POST)
		public Result add(@RequestBody @Validated Role entity,BindingResult validResult) throws EasyjException {
			if(validResult.hasErrors()){
				return Result.Error("数据不符合要求！");  
			}
								 
			int n=roleService.insertEntity(entity);
			return Result.Success(n);
			
		}
		
		@RequestMapping(value = "getPermissionTree", method = {RequestMethod.GET,RequestMethod.POST})
		public Result getPermissionTree() {
			return Result.Success(permissionService.getTree());
		}
		
		/**
		* 修改
		 * @throws EasyjException 
		*/
		@RequestMapping(value = "update", method = RequestMethod.POST)
		public Result update(@RequestBody @Validated Role entity,BindingResult validResult) throws EasyjException {
			if(validResult.hasErrors()){
				return Result.Error("数据不符合要求！");  
			}
							 
			int n=roleService.updateEntity(entity);
			return Result.Success(n);
			
		}
		/**
		* 查询
		* @param filter
		* @return
		 */
		@RequestMapping(value = "findList", method = RequestMethod.POST)
		public Result findList(@RequestBody  Role filter) {
			List<Role>  list =roleService.findList(filter);
			return Result.Success(list);
			
		}
		
        /**
 		 *删除
         * @throws EasyjException 
 		*/
 		@RequestMapping(value = "delete", method = {RequestMethod.POST,RequestMethod.GET})
 		public Result delete(@RequestParam(value = "id") Long id) throws EasyjException{
 			int n=roleService.deleteById(id);
 			return Result.Success(n);
 			
 		}
 		/**
 		 * 角色的权限
 		 * @param id
 		 * @return
 		 */
 		@RequestMapping(value = "getPermissions", method = RequestMethod.GET)
	    public Result getPermissions(@RequestParam(value = "id") Long id) {
 			ArrayList<Long> rtn=new ArrayList<Long>();
 			List<Permission> list = roleService.findPermissionsByRoleId(id);
 			if(list!=null) {
 				for(Permission p:list) {
 					rtn.add(p.getId());
 				}
 			}
	        return Result.Success(rtn);
	    }
 		
 		
 		@PostMapping("updatePermission/{id}")
	    public Result updatePermission(@PathVariable("id") Long id,@RequestBody Long[] permisionIds) throws EasyjException {
			roleService.updatePermision(id, permisionIds);
			return Result.Success("OK");
	    }
 		
 		@RequestMapping(value = "findUsersByRoleId", method = {RequestMethod.POST,RequestMethod.GET})
 		public Result findUsersByRoleId(@RequestParam(value = "id") Long id){					
 	    	return Result.Success(roleService.findUsersByRoleId(id));
 			
 			
 		}
		
}

